package com;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.anjiplus.template.gaea.business.modules.datasettransform.service.HttpResultTransform;
import org.example.quartz.service.LogService;
import org.springframework.jdbc.core.JdbcTemplate;

import java.time.LocalDateTime;

/**
 * 建议在idea写好复制整个类到此处，位置report-core/src/test/java/com/JavaResultTransform.java
 * 发送请求后 对返回值进行处理; 日志记录
 */
public class JavaResultTransform implements HttpResultTransform {

    @Override
    public String transform(String data) {
        System.err.println("JavaResultTransform:" + data);
        if (!data.isEmpty()) {
            //LogService日志接口
            LogService loginfo = SpringUtil.getBean(LogService.class);
            JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
            //转为JSONObject
            JSONObject jsonObject = JSONObject.parseObject(data);
            //获取返回参数
            String returnMSG = jsonObject.getString("result");
            //url
            String gsUrl = jsonObject.getString("apiUrl");
            //发送参数
            String GsxzJson = jsonObject.getString("requestData");
            //转为JSONObject
            JSONObject fsJson = JSONObject.parseObject(GsxzJson);
            //TS时间
            String TS = fsJson.getString("TS");
            String methodName = fsJson.getString("methodName");
            System.err.println(methodName + "返回参数{:" + returnMSG + "}");
            String YYID = fsJson.getString("YYID");
            // String YYID = "";
            if (returnMSG.contains("code=0")) {
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "成功", GsxzJson, returnMSG, "", YYID, gsUrl, TS);
                if (loginsert > 0) {
                    System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            } else if (returnMSG.contains("code=1")) {
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "失败", GsxzJson, returnMSG, "", YYID, gsUrl, TS);
                if (loginsert > 0) {
                    System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            } else {
                //写入日志表
                int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "数据返回异常", GsxzJson, returnMSG, "", YYID, gsUrl, TS);
                if (loginsert > 0) {
                    System.out.println("日志写入成功！！");
                } else {
                    System.out.println("日志写入失败！！");
                }
            }
        }
        return null;
    }
}